const jwt = require('jsonwebtoken')
// const { User } = require('../model/user')
const User = require('../model/user2')
const { code } = require('../static/secret')
const bcrypt = require('bcrypt')

module.exports = {
  getInfo: async (ctx, next) => {
    const token = ctx.header.authorization.split(' ')[1]
    const { username } = await jwt.verify(token, code)
    const userInfo = await User.findOne({ username: username }).lean()
    ctx.body = {}
    ctx.body.status = 200
    ctx.body.message = '用户信息获取成功'
    ctx.body.data = userInfo
    await next()
  },
  passwordChange: async (ctx, next) => {
    ctx.verifyParams({
      password: { type: 'string' }
    })
    const key = ctx.request.body.password
    const token = ctx.header.authorization.split(' ')[1]
    const { username } = await jwt.verify(token, code)
    const newPassword = await bcrypt.hash(key, 10)
    await User.updateOne({ username: username }, { password: newPassword })
    ctx.body = {
      status: 200,
      message: '更改成功',
      body: {}
    }
    await next()
  },
  saveStyle: async (ctx, next) => {
    ctx.verifyParams({
      style: { type: 'array' }
    })
    const token = ctx.header.authorization.split(' ')[1]
    const { style } = ctx.request.body
    const { username } = await jwt.verify(token, code)
    await User.updateOne({ username: username }, { style: style })
    ctx.body = {
      status: 200,
      message: ' 插入学习风格成功',
      data: {}
    }
  }
}
